﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using System.Diagnostics;

namespace BaGiaDanhCuop
{
    public partial class BuyPage : PhoneApplicationPage
    {
        ContentManager contentManager;
        GameTimer timer;
        SpriteBatch spriteBatch;
        UIElementRendererHelper uiRenderer;

        Texture2D Stick_Baseball_TT;
        Texture2D Stick_Golf_TT;
        Texture2D Stick_Light_TT;
        Texture2D Stick_Tennis_TT;
        Texture2D Hearth_TT, Key_TT, Medicine_TT, Power_TT;

        Texture2D Border_Up_TT, Border_Down_TT;

        Texture2D Baseball_Details;
        Texture2D Golf_Details;
        Texture2D Light_Details;
        Texture2D Tennis_Details;
        Texture2D Key_Details;
        Texture2D Hearth_Details;
        Texture2D Medicine_Details;
        Texture2D Energy_Drink_Details;

        //ItemShop BuyShop_Baseball, BuyShop_Golf, BuyShop_Light, BuyShop_Tennis;
        //ItemShop Shop_Hearth, Shop_Key, Shop_Medicine, Shop_Power;

        ItemShop[] item = new ItemShop[8];
        int[] check_buy = new int[8];

        // Constructor
        public BuyPage()
        {
            InitializeComponent();
            contentManager = (Application.Current as App).Content;

            // Create a timer for this page
            timer = new GameTimer();
            timer.UpdateInterval = TimeSpan.FromMilliseconds(5);
            timer.Update += OnUpdate;
            timer.Draw += OnDraw;

          

        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(true);
            spriteBatch = new SpriteBatch(SharedGraphicsDeviceManager.Current.GraphicsDevice);
            uiRenderer = new UIElementRendererHelper(this);
            

            timer.Start();
            LoadContent();
            base.OnNavigatedTo(e);
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            // Stop the timer
            timer.Stop();
            
            // Set the sharing mode of the graphics device to turn off XNA rendering
            SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode(false);
            uiRenderer.Dispose();
            base.OnNavigatedFrom(e);
        }

        //checktouch
        public void checkTouch()
        {
            TouchCollection touch = TouchPanel.GetState();
            foreach (TouchLocation tch in touch)
            {
                CheckButton(tch);
            }
        }

        public void CheckButton(TouchLocation touch)
        {
            if (touch.State == TouchLocationState.Pressed)
            {
                double x = touch.Position.X;
                double y = touch.Position.Y;

                //BuyShop_Baseball.CheckTouch(x, y);
                //BuyShop_Golf.CheckTouch(x, y);
                //BuyShop_Light.CheckTouch(x, y);
                //BuyShop_Tennis.CheckTouch(x, y);

                //Shop_Hearth.CheckTouch(x, y);
                //Shop_Key.CheckTouch(x, y);
                //Shop_Medicine.CheckTouch(x, y);
                //Shop_Power.CheckTouch(x, y);

                for (int i = 0; i < 8; i++)
                {
                    check_buy[i] = 0;
                    item[i].CheckTouch(x, y);
                    //Debug.WriteLine("item["+i+"].Click: "+item[i].isClick);
                    
                }
                for (int i = 0; i < 8; i++)
                {
                    if (item[i].isClick)
                    {
                        for (int j = 0; j < 8; j++)
                        {
                            if (j != i)
                            {
                                item[j].isSelect = false;
                            }
                        }
                    }
                    //Debug.WriteLine("item[" + i + "].Select: " + item[i].isSelect);
                    if (item[i].isSelect)
                    {
                        check_buy[i] = 1;
                    }
                }
            }
        }

        private void OnUpdate(object sender, GameTimerEventArgs e)
        {
            checkTouch();
            double delta = e.ElapsedTime.TotalSeconds;
            

        }

        /// <summary>
        /// Allows the page to draw itself.
        /// </summary>
        private void OnDraw(object sender, GameTimerEventArgs e)
        {
            SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Color.CornflowerBlue);
            uiRenderer.PreRender();
            uiRenderer.Draw();

            // TODO: Add your drawing code here
            spriteBatch.Begin();

            //BuyShop_Baseball.Draw(spriteBatch);
            //BuyShop_Golf.Draw(spriteBatch);
            //BuyShop_Light.Draw(spriteBatch);
            //BuyShop_Tennis.Draw(spriteBatch);

            //Shop_Hearth.Draw(spriteBatch);
            //Shop_Key.Draw(spriteBatch);
            //Shop_Medicine.Draw(spriteBatch);
            //Shop_Power.Draw(spriteBatch);

            for (int i = 0; i < 8; i++)
            {
                item[i].Draw(spriteBatch);
            }
            spriteBatch.DrawString(MainPage.ItemFont, "" + GamePage.money, new Vector2(400, 47), Color.Green);
            spriteBatch.DrawString(MainPage.ItemFont, "" + GamePage.coin, new Vector2(640, 47), Color.Yellow);
            //item
            spriteBatch.DrawString(MainPage.ItemFont, "" + GamePage.Woman.hearth, new Vector2(430, 380), Color.Yellow);
            spriteBatch.DrawString(MainPage.ItemFont, "" + GamePage.Woman.key, new Vector2(535, 380), Color.Blue);
            spriteBatch.DrawString(MainPage.ItemFont, "" + GamePage.Woman.medicine, new Vector2(640, 380), Color.Green);
            spriteBatch.DrawString(MainPage.ItemFont, "" + GamePage.Woman.energydrink, new Vector2(745, 380), Color.Red);
            
            spriteBatch.End();
        }

        void LoadContent()
        {
            Stick_Baseball_TT = contentManager.Load<Texture2D>("Asset/Baseball_Stick");
            Stick_Golf_TT = contentManager.Load<Texture2D>("Asset/Golf_Stick");
            Stick_Light_TT = contentManager.Load<Texture2D>("Asset/Light_Stick");
            Stick_Tennis_TT = contentManager.Load<Texture2D>("Asset/Tennis_Stick");
            Border_Up_TT = contentManager.Load<Texture2D>("Asset/Border_Buy_Up");
            Border_Down_TT = contentManager.Load<Texture2D>("Asset/Border_Buy_Down");
            Hearth_TT = contentManager.Load<Texture2D>("Asset/Hearth_Shop");
            Key_TT = contentManager.Load<Texture2D>("Asset/Key_Shop");
            Medicine_TT = contentManager.Load<Texture2D>("Asset/Medicine_Shop");
            Power_TT = contentManager.Load<Texture2D>("Asset/Power_Shop");

            Baseball_Details    = contentManager.Load<Texture2D>("Asset/Baseball_Details");
            Golf_Details        = contentManager.Load<Texture2D>("Asset/Golf_Details");
            Light_Details       = contentManager.Load<Texture2D>("Asset/Light_Details");
            Tennis_Details      = contentManager.Load<Texture2D>("Asset/Tennis_Details");
            Key_Details         = contentManager.Load<Texture2D>("Asset/Key_Details");
            Hearth_Details      = contentManager.Load<Texture2D>("Asset/Hearth_Details");
            Medicine_Details    = contentManager.Load<Texture2D>("Asset/Medicine_Details");
            Energy_Drink_Details = contentManager.Load<Texture2D>("Asset/Energy_Drink_Details");



            //BuyShop_Tennis      = new ItemShop(Stick_Tennis_TT, Border_Up_TT, Tennis_Details, 350, 96);
            //BuyShop_Baseball    = new ItemShop(Stick_Baseball_TT, Border_Up_TT, Baseball_Details, 455, 96);
            //BuyShop_Golf        = new ItemShop(Stick_Golf_TT, Border_Up_TT, Golf_Details, 560, 96);
            //BuyShop_Light       = new ItemShop(Stick_Light_TT, Border_Up_TT, Light_Details, 665, 96);
            //Shop_Hearth         = new ItemShop(Hearth_TT, Border_Down_TT, Hearth_Details, 352, 330);
            //Shop_Key            = new ItemShop(Key_TT, Border_Down_TT, Key_Details, 457, 330);
            //Shop_Medicine       = new ItemShop(Medicine_TT, Border_Down_TT, Medicine_Details, 562, 330);
            //Shop_Power          = new ItemShop(Power_TT, Border_Down_TT, Energy_Drink_Details, 667, 330);

            item[0] = new ItemShop(Stick_Tennis_TT, Border_Up_TT, Tennis_Details, 1000, 350, 96);
            item[1] = new ItemShop(Stick_Baseball_TT, Border_Up_TT, Baseball_Details, 3500, 455, 96);
            item[2] = new ItemShop(Stick_Golf_TT, Border_Up_TT, Golf_Details,4500, 560, 96);
            item[3] = new ItemShop(Stick_Light_TT, Border_Up_TT, Light_Details, 8000, 665, 96);
            item[4] = new ItemShop(Hearth_TT, Border_Down_TT, Hearth_Details, 550, 352, 330);
            item[5] = new ItemShop(Key_TT, Border_Down_TT, Key_Details, 450, 457, 330);
            item[6] = new ItemShop(Medicine_TT, Border_Down_TT, Medicine_Details, 450, 562, 330);
            item[7] = new ItemShop(Power_TT, Border_Down_TT, Energy_Drink_Details, 500, 667, 330);
        }


        private void Bt_Back_BuyPage_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.GoBack();
        }

        private void Bt_Buy_BuyPage_Click(object sender, RoutedEventArgs e)
        {
            // buy item
            for (int i = 0; i < 4; i++)
            {
                //Debug.WriteLine("check["+i+"]: "+check_buy[i]);
                if (check_buy[i] == 1 && GamePage.money - item[i].price >= 0)
                {
                    item[i].isBuy = true;
                    GamePage.Woman.item = i+1;
                }
                else if (check_buy[i] == 1 && GamePage.money - item[i].price < 0)
                {
                    MessageBox.Show("Not enough money!");
                }
            }

            //-----------------------------------------
            if (check_buy[4] == 1 && GamePage.Woman.hearth < 3 && GamePage.coin - item[4].price >= 0)
            {
                GamePage.Woman.hearth += 1;
            }
            else if (check_buy[4] == 1 && GamePage.Woman.hearth >= 3)
            {
                MessageBox.Show("Full heart");
            }
            else if (check_buy[4] == 1 && GamePage.coin - item[4].price < 0)
            {
                MessageBox.Show("Not enough coin!");
            }

             //-----------------------------------------
            else if (check_buy[5] == 1 && GamePage.Woman.key < 3 && GamePage.coin - item[5].price >= 0)
            {
                GamePage.Woman.key += 1;
            }
            else if (check_buy[5] == 1 && GamePage.Woman.key >= 3)
            {
                MessageBox.Show("Full key");
            }
            else if (check_buy[5] == 1 && GamePage.coin - item[5].price < 0)
            {
                MessageBox.Show("Not enough coin!");
            }

                 //-----------------------------------------
            else if (check_buy[6] == 1 && GamePage.Woman.medicine < 3 && GamePage.coin - item[6].price >= 0)
            {
                GamePage.Woman.medicine += 1;
            }
            else if (check_buy[6] == 1 && GamePage.Woman.medicine >= 3)
            {
                MessageBox.Show("Full medicine");
            }
            else if (check_buy[6] == 1 && GamePage.coin - item[6].price < 0)
            {
                MessageBox.Show("Not enough coin!");
            }

                 //-----------------------------------------
            else if (check_buy[7] == 1 && GamePage.Woman.energydrink < 3 && GamePage.coin - item[7].price >= 0)
            {
                GamePage.Woman.energydrink += 1;
            }
            else if (check_buy[7] == 1 && GamePage.Woman.energydrink >= 3)
            {
                MessageBox.Show("Full energy drink");
            }
            else if (check_buy[7] == 1 && GamePage.coin - item[7].price < 0)
            {
                MessageBox.Show("Not enough coin!");
            }
        }
    }
}